package com.nny.alarm.eidt;

import com.nny.alarm.R;
import com.nny.alarm.view.DialTimeView;
import com.nny.alarm.view.DialViewAdapter;
import com.nny.alarm.view.DigitalTimeView;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.TimeCycle;
import com.uxwine.alarm.TimeRecord;

public class TimeEditor implements IAlarmEditor
{
	AlarmEditor mEditor;
	DialViewAdapter mAdapter;
	
	public TimeEditor(AlarmEditor a)
	{
		mEditor = a;
	}

	@Override
	public void edit()
	{
		final DigitalTimeView dttv = mEditor.mDigital;
		final DialTimeView dtv = mEditor.mDial;
		
		//更新数字钟界面：包括设置mark,设置enable和设置时间		
		dttv.update(mEditor.mAlarm);
		dttv.updateDigitals();
		
		mAdapter = createAdapter(dttv.first(), mEditor.mAlarm);
		dtv.setAdpater(mAdapter);
		mAdapter.notifyDataChanged();
	}

	@Override
	public boolean next()
	{
		final DialTimeView dtv = mEditor.mDial;
		final DigitalTimeView dttv = mEditor.mDigital;
		int key = dttv.next();
		if(TimeRecord.SECOND != key)
		{
			mAdapter = createAdapter(key, mEditor.mAlarm);
			dtv.setAdpater(mAdapter);
			mAdapter.notifyDataChanged();
		}
		else
		{
			mEditor.step(AlarmEditor.STEP_RING);
		}
		return true;
	}

	@Override
	public void onMarked(int index)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMarkChange(int index)
	{
		final DigitalTimeView dttv = mEditor.mDigital;
		dttv.set(dttv.current(), (int)mAdapter.getValue(index));
	}	
	
	DialViewAdapter createAdapter(int key, Alarm a)
	{
		TimeRecord time = a.time;
		TimeCycle cycle = a.cycle;
		ZoneDVAdpater adapter;
		switch(key)
		{
		case TimeRecord.YEAR:
		{
			int year = time.get(TimeRecord.YEAR);
			adapter = new ZoneDVAdpater(year, year + 7);
			adapter.setPosition(adapter.getPosition(year));
			adapter.setButtonId(R.drawable.time_dial_year);
			return adapter;
		}
		case TimeRecord.MONTH:
		{
			int month = time.get(TimeRecord.MONTH);
			adapter = new ZoneDVAdpater(1, 12);
			adapter.setPosition(adapter.getPosition(month + 1));
			adapter.setButtonId(R.drawable.time_dial_moth);
			return adapter;
		}
		case TimeRecord.DATE:
		{
			int day = time.get(TimeRecord.DATE);
			if(cycle.isEveryMonth())
			{
				adapter = new ZoneDVAdpater(1, 31);				
			}
			else if(cycle.isEveryYear() || cycle.isOnce())
			{
				int year = time.get(TimeRecord.YEAR);
				int month = time.get(TimeRecord.MONTH);
				adapter = new ZoneDVAdpater(1, 
						getDaysOfMonth(year, month + 1, cycle.isOnce()));
			}
			else
			{
				throw new IllegalArgumentException();
			}
			adapter.setButtonId(R.drawable.time_dial_date);
			adapter.setPosition(adapter.getPosition(day + 1));
			return adapter;
		}
		case TimeRecord.HOUR:
		{
			int hour = time.get(TimeRecord.HOUR);
			adapter = new ZoneDVAdpater(0, 23);
			adapter.setPosition(adapter.getPosition(hour));
			adapter.setButtonId(R.drawable.time_dial_hour);
			return adapter;
		}
		case TimeRecord.MINUTE:
		{
			int minute = time.get(TimeRecord.MINUTE);
			adapter = new ZoneDVAdpater(0, 59);
			adapter.setPosition(adapter.getPosition(minute));
			adapter.setButtonId(R.drawable.time_dial_minute);
			return adapter;
		}
		default:
			throw new IllegalArgumentException();
		}
	}
	
	public int getDaysOfMonth(int year, int month, boolean isOnce)
	{
		switch(month)
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
			return (isOnce || TimeCycle.isLeap(year)) ? 29 : 28;
		}
		return 30;
	}

	@Override
	public void over()
	{
		// TODO Auto-generated method stub		
	}
}
